package com.myjavalab.dotsandboxes;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Computer {

	private Board board;
	private Map<String, Box> zeroEdgeMap;
	private Map<String, Box> oneEdgeMap;
	private Map<String, Box> twoEdgeMap;
	private Map<String, Box> threeEdgeMap;
	private Map<String, Box> fourEdgeMap;
	
	public Computer(Board board){
		this.board = board;
		initBoxMap();
	}
	
	public void play(){
		updateEdgeMap(threeEdgeMap);
		updateEdgeMap(twoEdgeMap);
		updateEdgeMap(oneEdgeMap);
		updateEdgeMap(zeroEdgeMap);

		guess();
	}
	
	private void updateEdgeMap(Map<String, Box> map){
		if(map.size() > 0){
			Map<String, Box> copyMap = new HashMap<String,Box>();
			copyMap.putAll(map);
			Iterator<Box> it = copyMap.values().iterator();
			while(it.hasNext()){
				Box b = it.next();
				int sum = 0;
				int row = b.getRow();
				int column = b.getColumn();
				int edgeNum = b.getEdgeNum();
				String name = b.getName();
				char[][] m = board.getModel();
				if (m[row - 1][column] != Board.BLANK) {
					sum++;
				}
				if (m[row + 1][column] != Board.BLANK) {
					sum++;
				}
				if (m[row][column - 1] != Board.BLANK) {
					sum++;
				}
				if (m[row][column + 1] != Board.BLANK) {
					sum++;
				}
				if (sum > edgeNum) {
					map.get(name).setEdgeNum(sum);
					switch (sum){
						case 4 : fourEdgeMap.put(name, map.get(name)); break; 
						case 3 : threeEdgeMap.put(name, map.get(name));break; 
						case 2 : twoEdgeMap.put(name, map.get(name)); break;
						case 1 : oneEdgeMap.put(name, map.get(name));break;
					}
					map.remove(name);

				}

			}
		}
		
	}
	
	private void guess(){
		int row = 0;
		int column = 0;
		char[][] m = board.getModel();
		if(threeEdgeMap.size() > 0) {
			Box b = threeEdgeMap.values().iterator().next();
			row = b.getRow();
			column = b.getColumn();
			if (m[row - 1][column] == Board.BLANK) {
				row--;
			}else if (m[row + 1][column] == Board.BLANK) {
				row++;
			}else if (m[row][column - 1] == Board.BLANK) {
				column--;
			}else if (m[row][column + 1] == Board.BLANK) {
				column++;
			}
		}else if(zeroEdgeMap.size() > 0){
			Box b = zeroEdgeMap.values().iterator().next();
			row = b.getRow();
			column = b.getColumn();
			if (m[row - 1][column] == Board.BLANK) {
				row--;
			}
		}else if(oneEdgeMap.size() > 0){
			Box b = oneEdgeMap.values().iterator().next();
			row = b.getRow();
			column = b.getColumn();
			if (m[row - 1][column] == Board.BLANK) {
				row--;
			}else if (m[row + 1][column] == Board.BLANK) {
				row++;
			}
		}else if(twoEdgeMap.size() > 0){
			Box b = twoEdgeMap.values().iterator().next();
			row = b.getRow();
			column = b.getColumn();
			if (m[row - 1][column] == Board.BLANK) {
				row--;
			}else if (m[row + 1][column] == Board.BLANK) {
				row++;
			}else if (m[row][column - 1] == Board.BLANK) {
				column--;
			}
		}
		System.out.println("computer enter (" + row + ", " + column + ")");
		board.update(row, column);
	}
	private void initBoxMap(){
		zeroEdgeMap = new HashMap<String, Box>();
		oneEdgeMap = new HashMap<String, Box>();
		twoEdgeMap = new HashMap<String, Box>();
		threeEdgeMap = new HashMap<String, Box>();
		fourEdgeMap = new HashMap<String, Box>();
		
		for(int i = 1; i < board.getWidth(); i = i + 2){
			for(int j = 1; j < board.getWidth(); j = j + 2){
				String name = i + "" + j;
				zeroEdgeMap.put(name, new Box(i,j,name));		
			}
		}
	}
}
